package com.azkj.album.view.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.azkj.album.R;
import com.azkj.album.network.Constants;
import com.azkj.album.utils.DensityUtils;
import com.azkj.album.view.activity.WebViewActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class ProtocolDialog {


    public static final class Builder {

        private Context mContext;
        private final Dialog dialog;
        private CallBack mCallback;

        @BindView(R.id.tv_yyb_desc)
        TextView mTvDesc;

        public Builder(Context context) {
            this.mContext = context;
            View view = LayoutInflater.from(context).inflate(
                    R.layout.dialog_yyb_protocol, null);
            ButterKnife.bind(this, view);

            // 定义Dialog布局和参数
            dialog = new Dialog(context, R.style.MyDialog);
            dialog.setContentView(view);
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(Gravity.CENTER);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.x = 0;
            lp.y = 0;
            lp.width = (int) (DensityUtils.getScreenWidth(context));
            lp.height = (int) (DensityUtils.getScreenHeight(context));
            lp.gravity = Gravity.CENTER;
            dialogWindow.setAttributes(lp);
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);

            setDesc();
        }


        public Builder show() {
            dialog.show();
            return this;

        }


        public Builder setCallback(CallBack callBack) {
            this.mCallback = callBack;
            return this;

        }


        @OnClick({R.id.tv_yyb_close, R.id.tv_yyb_agree})
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_yyb_close:
                    dialog.dismiss();
                    if (mCallback != null)
                        mCallback.onAgree(false);
                    break;
                case R.id.tv_yyb_agree:
                    dialog.dismiss();
                    if (mCallback != null)
                        mCallback.onAgree(true);
                    break;

            }
        }

        private void setDesc() {
            String content = mContext.getString(R.string.protocol);
            SpannableStringBuilder builder = new SpannableStringBuilder(content);

            int index = 0;
            while (content.indexOf("《", index) != -1) {
                int atIndex = content.indexOf("《", index);  //《的位置
                int sIndex = content.indexOf("》", atIndex); //》空格的位置

                index = sIndex;
                if (sIndex < atIndex) { //格式不符合
                    continue;
                }
                Log.d("ClickSpan", "《 = " + atIndex + " 》 = " + sIndex);

                CustomClickableSpan clickableSpan = new CustomClickableSpan(atIndex);
                builder.setSpan(clickableSpan, atIndex, sIndex + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }

            mTvDesc.setMovementMethod(LinkMovementMethod.getInstance());
            mTvDesc.setText(builder);
            mTvDesc.setLineSpacing(1, 1.5f);
            mTvDesc.setHighlightColor(mContext.getResources().getColor(android.R.color.transparent));
        }

        class CustomClickableSpan extends ClickableSpan {

            private int mAtIndex;

            public CustomClickableSpan(int atIndex) {
                mAtIndex = atIndex;
            }

            @Override
            public void onClick(View widget) {

                if (widget instanceof TextView) {
                    TextView textView = (TextView) widget;

//                    textView.setHighlightColor(mContext.getResources().getColor(android.R.color.transparent));

                    String str = textView.getText().toString();
                    int sIndex = str.indexOf("》", mAtIndex);

                    Intent intent = new Intent(mContext, WebViewActivity.class);

                    if ("隐私政策".equals(str.substring(mAtIndex + 1, sIndex))) {
                        intent.putExtra("title", "隐私政策");
                        intent.putExtra("url", Constants.WEB_PAGE_PROTOCOL_1);
                        mContext.startActivity(intent);
                    } else if ("用户协议".equals(str.substring(mAtIndex + 1, sIndex))) {
                        intent.putExtra("title", "用户协议");
                        intent.putExtra("url", Constants.WEB_PAGE_PROTOCOL_2);
                        mContext.startActivity(intent);
                    }


                }
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(mContext.getResources().getColor(R.color.lanse));
                ds.clearShadowLayer();
                ds.setUnderlineText(false);
            }
        }


    }

    public interface CallBack {
        void onAgree(boolean agree);
    }


}
